package com.cqu.lpss.controller;

import com.cqu.lpss.pojo.Administors;
import com.cqu.lpss.pojo.Contestants;
import com.cqu.lpss.pojo.Professors;
import com.cqu.lpss.pojo.Result;
import com.cqu.lpss.service.impl.AdministorsImpl;
import com.cqu.lpss.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@Slf4j
@RequestMapping("/administors")
public class AdministorsController {
    @Autowired
    AdministorsImpl administorImpl;
    @PostMapping("/login")
    public Result login(@RequestBody Administors administor){
        log.info("参赛人员登录：{}",administor);
        Administors loginuser = administorImpl.login(administor);
        //登录成功生成jwt令牌
        if(loginuser!=null){
            HashMap<String, Object> claims = new HashMap<>();
            claims.put("userIndex",loginuser.getAdmIndex());
            claims.put("userId",loginuser.getAdmId());
            claims.put("userName",loginuser.getAdmName());
            claims.put("userClass",null);
            claims.put("roleName","admin");

            String jwt = JwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }
        //登录失败，返回错误信息
        return Result.erro("用户名或密码错误");
    }
    @GetMapping("/findById")
    public Result findById(@RequestParam String userId){
        Administors administors = administorImpl.findById(userId);
        if(administors!=null){
            return  Result.success(administors);
        }
        return  Result.erro("未知错误");
    }
    @PostMapping("/PersonCenter_upload")
    public Result PersonCenter_upload(@RequestParam String newPassword,@RequestParam String phone,@RequestParam String email,@RequestParam String id){
        int ans = administorImpl.PersonCenter_upload(newPassword,phone,email, id);
        if(ans!=0){
            return  Result.success();
        }
        else {
            return Result.erro("密码重置失败，请联系管理员");
        }
    }
}
